package dataStructure.tree;

//顺序存储完全二叉树
public class ArrBinaryTreeDemo {

    public static void main(String[] args) {

        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(new int[]{1, 2, 3, 4, 5, 6, 7});
        arrBinaryTree.infixOrder();
    }


}
class ArrBinaryTree{
    private int[] arr;//存储数据结点的数组
    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }
    public void preOrder(){
        this.preOrder(0); //方法重载
    }
    public void infixOrder(){
        this.infixOrder(0);
    }

    //编写一个方法，完成顺序存储二叉树的前序遍历
    public void preOrder(int index){
        if(arr == null || arr.length == 0) {
            System.out.println("数组为空，不能按照二叉树的前序遍历");
        }
        System.out.println(arr[index]);

        if ((index*2+1)<arr.length) {
            preOrder(index * 2 + 1);
        }
        if ((index*2+2)<arr.length) {
            preOrder(index * 2 + 2);
        }
    }

    public void infixOrder(int index){
        if(arr == null || arr.length == 0) {
            System.out.println("数组为空，不能按照二叉树的前序遍历");
        }
        if ((index*2+1)<arr.length) {
            infixOrder(index * 2 + 1);
        }
        System.out.println(arr[index]);
        if ((index*2+2)<arr.length) {
            infixOrder(index * 2 + 2);
        }
    }

}